import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:lcapp/api/interface/home_api.dart';
import 'package:lcapp/api/request/response_data.dart';
import 'package:lcapp/data/xian_huo_data_entity.dart';
import 'package:lcapp/model/level_picker/level_picker.dart';
import 'package:lcapp/model/toast/toast.dart';
import 'package:lcapp/routes/route.dart';
import 'package:lcapp/tools/input_filter.dart';

class MinXHChangePrice extends StatefulWidget {
  final Map  arguments; //0 出售 1 采购
  const MinXHChangePrice({Key? key, required this.arguments}) : super(key: key);

  @override
  State<MinXHChangePrice> createState() => _MinXHChangePriceState();
}

class _MinXHChangePriceState extends State<MinXHChangePrice> {

  TextEditingController pricecontroller = TextEditingController();
  TextEditingController numcontroller = TextEditingController();
  List dwList = ['/每斤','/每吨'];
  String dwStr = '/每斤';
  XianHuoDataEntity entity = XianHuoDataEntity();
  void initState() {
    // TODO: implement initState
    super.initState();
     entity = widget.arguments['entity'];
     dwStr = entity.productUnit == 1?'元/每斤':'元/每吨';
     pricecontroller.text = entity.price!;
    numcontroller.text = entity.numbers!;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.transparent,
      body: Stack(
        children: [
          Positioned(child: SingleChildScrollView(
            // backgroundColor: Colors.transparent,
            child: Container(
              padding: EdgeInsets.only(left: 40.w,right: 40.w,bottom: 20.w,top: 50.w),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.only(topLeft: Radius.circular(10.w),topRight: Radius.circular(10.w)),
              ),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                mainAxisAlignment: MainAxisAlignment.end,
                children: [
                  Container(
                    margin: EdgeInsets.only(bottom: 50.w),
                    child: Text('更新价格',style: TextStyle(
                        fontSize: 34.sp,
                        fontWeight: FontWeight.w500
                    ),),
                  ),
                  Container(
                    margin: EdgeInsets.only(bottom: 20.w),
                    padding: EdgeInsets.symmetric(horizontal: 20.w),
                    decoration: BoxDecoration(
                        color: Color(0xffF8F8F8),
                        borderRadius: BorderRadius.circular(6.w)
                    ),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Expanded(child: GestureDetector(
                          onTap: (){
                            FocusScope.of(context).requestFocus(FocusNode());
                          },
                          child: Container(
                              alignment: Alignment.centerLeft,
                              child: TextField(
                                controller: pricecontroller,
                                maxLines: 1,
                                keyboardType: TextInputType.numberWithOptions(decimal: true),
                                inputFormatters: [
                                  PrecisionLimitFormatter(2),
                                  FilteringTextInputFormatter.allow(RegExp('[0-9.,]+'))
                                ],
                                autocorrect: true,
                                autofocus: false,
                                textAlign: TextAlign.left,
                                decoration:  InputDecoration(
                                  contentPadding: EdgeInsets.only(left: 17.w),
                                  hintText: "价格（例：500/吨）",
                                  hintStyle: TextStyle(
                                    color: const Color(0xFFAEAEAE),
                                    fontSize: 26.sp,
                                    fontWeight: FontWeight.w500,
                                  ),
                                  counterText: "",
                                  border: const OutlineInputBorder(
                                    borderSide: BorderSide.none,
                                  ),
                                  fillColor: Colors.transparent,
                                  filled: true,
                                ),
                                onChanged: (value){
                                  entity.price = value.trim();
                                },
                              )
                          ),
                        )),
                        Container(
                          width: 1,
                          height: 35.w,
                          color: Color(0xff999999),
                          margin: EdgeInsets.symmetric(horizontal: 20.w),
                        ),
                        GestureDetector(
                          onTap: (){
                            FocusScope.of(ASRRoute.navigatorKey.currentContext!).requestFocus(FocusNode());
                            JhPickerTool.showStringPicker(
                              ASRRoute.navigatorKey.currentContext!,
                              title: '请选择单位',
                              data: dwList,
                              clickCallBack: _selectDWSex,
                              // pickerType: AsrPickerType.PICKERORANGETHEME,
                            );
                          },
                          child: Row(
                            children: [
                              Text(dwStr,style: TextStyle(
                                  color: Color(0xff333333),
                                  fontSize: 26.sp,
                                  fontWeight: FontWeight.w500
                              ),),
                              Icon(Icons.arrow_drop_down,size: 35.sp,color: Color(0xff333333),)
                            ],
                          ),
                        )
                      ],
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.only(bottom: 20.w),
                    padding: EdgeInsets.symmetric(horizontal: 20.w),
                    decoration: BoxDecoration(
                        color: Color(0xffF8F8F8),
                        borderRadius: BorderRadius.circular(6.w)
                    ),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Expanded(child: GestureDetector(
                          onTap: (){
                            FocusScope.of(context).requestFocus(FocusNode());
                          },
                          child: Container(
                              alignment: Alignment.centerLeft,
                              child: TextField(
                                controller: numcontroller,
                                maxLines: 1,
                                keyboardType: TextInputType.numberWithOptions(decimal: true),
                                inputFormatters: [
                                  PrecisionLimitFormatter(2),
                                  FilteringTextInputFormatter.allow(RegExp('[0-9.,]+'))
                                ],
                                autocorrect: true,
                                autofocus: false,
                                textAlign: TextAlign.left,
                                decoration:  InputDecoration(
                                  contentPadding: EdgeInsets.only(left: 17.w),
                                  hintText: "数量（例：200/吨）",
                                  hintStyle: TextStyle(
                                    color: const Color(0xFFAEAEAE),
                                    fontSize: 26.sp,
                                    fontWeight: FontWeight.w500,
                                  ),
                                  counterText: "",
                                  border: const OutlineInputBorder(
                                    borderSide: BorderSide.none,
                                  ),
                                  fillColor: Colors.transparent,
                                  filled: true,
                                ),
                                onChanged: (value){
                                  entity.numbers = value.trim();
                                },
                              )
                          ),
                        )),
                        Container(
                          padding: EdgeInsets.symmetric(horizontal: 20.w),
                          child: Text('吨',style: TextStyle(
                            color: Color(0xff666666),
                              fontSize: 26.sp,
                              fontWeight: FontWeight.w500
                          ),),
                        )
                      ],
                    ),
                  ),
                  Container(
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: [
                        GestureDetector(
                          onTap: (){
                            Navigator.of(context).pop();
                          },
                          child: Container(
                            width:310.w,
                            height: 90.w,
                            alignment: Alignment.center,
                            decoration: BoxDecoration(
                                color: Color(0xffEBEBEB),
                                borderRadius: BorderRadius.circular(10.w)
                            ),
                            child: Text('取消',style: TextStyle(
                                color: Color(0xff232323),
                                fontSize:30.sp,
                                fontWeight: FontWeight.w500
                            ),),
                          ),
                        ),
                        GestureDetector(
                          onTap: (){
                            savePrice();
                          },
                          child: Container(
                            width:310.w,
                            height: 90.w,
                            alignment: Alignment.center,
                            decoration: BoxDecoration(
                                color: Color(0xffFA6E28),
                                borderRadius: BorderRadius.circular(10.w)
                            ),
                            child: Text('更新',style: TextStyle(
                                color: Colors.white,
                                fontSize:30.sp,
                                fontWeight: FontWeight.w500
                            ),),
                          ),
                        )
                      ],
                    ),
                  ),
                ],
              ),
            ),
          ),bottom: 0,left: 0,right: 0,)
        ],
      ),
    );
  }
  _selectDWSex(int index, dynamic selectDate){
    setState(() {
      dwStr = selectDate;
    });
  }

  savePrice()async{
    showLoading();
    ResponseData res = await HomeAPI.changeXianhuoPrice(entity.id!,entity.numbers!,entity.price!,dwStr=='/每斤'?1:2);
    hideLoading();
    if(res.code==200){
      showToast('更新成功');
      entity.productUnit = dwStr=='/每斤'?1:2;
      if(widget.arguments['callback']!=null){
        widget.arguments['callback']();
      }
      Navigator.of(context).pop();
    }
  }

}
